<template>
  <div class="user-profile">
    <input type="file" hidden ref="inputFile" @change="inputChange" />
    <!-- 编辑头像弹层 -->
    <van-popup
      v-model="isShowUpdateAvatar"
      style="height: 100%"
      position="bottom"
    >
      <update-avatar
        :img="img"
        @close="isShowUpdateAvatar = false"
        v-if="isShowUpdateAvatar"
        @update-avatar="user.photo = $event"
      />
    </van-popup>
    <!-- 编辑头像弹层 -->
    <!-- 编辑昵称弹层 -->
    <van-popup v-model="isShowUpdateName" style="height: 50%" position="bottom">
      <updateName
        v-if="isShowUpdateName"
        v-model="user.name"
        @close="isShowUpdateName = false"
      ></updateName>
    </van-popup>

    <!-- 编辑昵称弹层 -->

    <!-- 编辑性别弹出层 -->
    <van-popup
      v-model="isShowUpdateGender"
      style="height: 50%"
      position="bottom"
    >
      <updateGender
        v-if="isShowUpdateGender"
        v-model="user.gender"
        @close="isShowUpdateGender = false"
      ></updateGender>
    </van-popup>

    <!-- 编辑性别弹出层 -->
    <!-- 生日弹出层 -->
    <van-popup
      v-model="isShowUpdateBirthday"
      style="height: 50%"
      position="bottom"
    >
      <updateBirthday
        v-model="user.birthday"
        @close="isShowUpdateBirthday = false"
      ></updateBirthday>
    </van-popup>

    <!-- 生日弹出层 -->
    <!-- 导航栏 -->
    <van-nav-bar
      class="page-nav-bar"
      title="个人信息"
      left-arrow
      @click-left="$router.back()"
      left-text="返回"
    />

    <!-- /导航栏 -->
    <!-- 模板绑定 -->
    <van-cell title="头像" is-link @click="$refs.inputFile.click()">
      <van-image class="avatar" fit="cover" round :src="user.photo" />
    </van-cell>
    <van-cell
      title="昵称"
      :value="user.name"
      is-link
      @click="isShowUpdateName = true"
    />
    <van-cell
      title="性别"
      :value="user.gender === 0 ? '男' : '女'"
      is-link
      @click="isShowUpdateGender = true"
    />
    <van-cell
      title="生日"
      :value="user.birthday"
      is-link
      @click="isShowUpdateBirthday = true"
    />
    <!-- 模板绑定 -->
  </div>
</template>

<script>
import { getUserProfile } from "@/api/user.js";
import updateAvatar from "./components/update-avatar.vue";
import updateName from "./components/update-name.vue";
import updateGender from "./components/update-gender.vue";
import updateBirthday from "./components/update-birthday.vue";

export default {
  name: "UserProfile",
  data() {
    return {
      user: {},
      isShowUpdateAvatar: false,
      img: null,
      isShowUpdateName: false,
      isShowUpdateGender: false,
      isShowUpdateBirthday: false,
    };
  },
  components: {
    updateAvatar,
    updateName,
    updateGender,
    updateBirthday,
  },

  created() {
    this.loadProfile();
  },

  methods: {
    async loadProfile() {
      try {
        const res = await getUserProfile();
        console.log("返回的个人资料是", res);
        this.user = res.data.data;
      } catch (err) {
        this.$toast("获取数据失败");
      }
    },
    // 编辑用户头像
    inputChange() {
      // 获取文件对象
      const file = this.$refs.inputFile.files[0];
      // console.log(this.$refs.inputFile.files);
      // 获取blob数据
      const imgUrl = window.URL.createObjectURL(file);
      console.log("预览地址是", imgUrl);
      this.img = imgUrl;
      this.isShowUpdateAvatar = true;
      this.$refs.inputFile.value = "";
    },
  },
};
</script>

<style scoped lang='less'>
.user-profile {
  .avatar {
    width: 60px;
    height: 60px;
  }
  /deep/ .page-nav-bar {
    .van-nav-bar__content {
      .van-nav-bar__left {
        .van-icon {
          color: #fff;
        }
        .van-nav-bar__text {
          color: #fff;
        }
      }
    }
  }
}
</style>